package com.module.home.ui.scoringFactors

import android.view.View
import androidx.core.content.ContextCompat
import com.common.base.app.extras.getShowMsg
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.setVisibility
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.app.fragment.BaseVMFragment
import com.common.base.utils.DividePageUtils
import com.common.base.widget.CustomTextView
import com.common.base.widget.animator.CustomAnimation1
import com.module.home.R
import com.module.home.databinding.HomeFragmentScoringFactorsBinding
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/08/27
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class ScoringFactorsFragment :
    BaseVMFragment<HomeFragmentScoringFactorsBinding, ScoringFactorsViewModel>() {

    companion object {
        fun newInstance(mType: String, mDate: String): ScoringFactorsFragment {
            val fragment = ScoringFactorsFragment()
            fragment.mType = mType
            fragment.mDate = mDate
            return fragment
        }
    }

    private val classicsFooter by lazy { ClassicsFooter(requireActivity()) }

    private lateinit var headView: View

    private val mAdapter by lazy { ScoringFactorsCardAdapter() }

    private var mType: String = ""

    private var mDate: String? = ""

    override fun getPlaceholderView() = mViewBinding.placeView

    override fun getViewModel() = ScoringFactorsViewModel::class.java

    override fun getViewBinding() = HomeFragmentScoringFactorsBinding.inflate(layoutInflater)

    override fun initViews() {
        super.initViews()

        headView = layoutInflater.inflate(R.layout.home_scoring_factors_head_layout, null)

        //上下拉刷新
        getRefreshFooter()?.let { mViewBinding.pdView.setRefreshFooter(it) }
        mViewBinding.pdView.setRefreshHeader(
            ClassicsHeader(requireActivity()).setAccentColor(
                ContextCompat.getColor(requireActivity(), R.color.text_content_week)
            )
        )
        classicsFooter.setAccentColor(
            ContextCompat.getColor(
                requireActivity(),
                R.color.color_999999
            )
        )
        mViewBinding.pdView.setRefreshFooter(classicsFooter)

        mAdapter.apply {
            addHeaderView(headView)
            addFooterView(layoutInflater.inflate(R.layout.base_rlv_margin_footer_tr_view50, null))
            adapterAnimation = CustomAnimation1()
            isAnimationFirstOnly = true
//            isAnimationFirstOnly = false
            mViewBinding.hrv.adapter = this
        }

        headView.findViewById<CustomTextView>(R.id.tvScoringFactorsTitle).text =
            "$mType${getString(R.string.home_ac_scoring_factors_title)}"
    }

    override fun lazyInitData() {
        super.lazyInitData()
        mViewBinding.pdView.setEnableLoadMoreWhenContentNotFull(false)
        mViewBinding.pdView.setEnableLoadMore(false)

        getDataList()
    }


    override fun initEvents() {
        super.initEvents()
        setErrorRetryListener {
            lazyInitData()
        }

        mViewBinding.pdView.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                lazyInitData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {

            }
        })
    }

    private fun finishPdView() {
        mViewBinding.pdView.finishRefresh()
        mViewBinding.pdView.finishLoadMore()
    }

    private fun getDataList() {
        if (mDate.isNullOrEmpty()) {
            showErrorMsg()
            return
        }
        showDialogLoading()

        mViewModel.getSleepScoringFactors(
            when (mType) {
                getString(R.string.common_sleep_hint) -> 1
                getString(R.string.common_exercise_hint) -> 2
                getString(R.string.common_signs_hint) -> 3
                else -> 0
            }, mDate!!
        ) {
            finishPdView()
            showToast(it.getShowMsg())
        }.observe(this) {
            finishPdView()
            dismissLoadingDialog()
            it.isSuccess().yes {
                it.isSuccess().yes {
                    it.Data?.let { dataIt ->
                        headView.findViewById<CustomTextView>(R.id.tvScore).text =
                            dataIt.total_score.toInt().toString()
                        headView.findViewById<CustomTextView>(R.id.tvTime).text = mDate ?: ""
                        headView.findViewById<CustomTextView>(R.id.tvScoringFactorsBody).text =
                            dataIt.ai_analyza
                        mAdapter.setList(dataIt.score_types)
                    }
                }.otherwise {
                    showToast(it.Message)
                }
            }.otherwise {
                showToast(it.Message)
            }
        }
    }
}